package helpers;

import models.App;
import models.AppGroup;
import models.Topic;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import play.i18n.Lang;
import play.mvc.Http;
import play.mvc.Router;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: GIPXY
 * Date: 5/14/13
 * Time: 4:35 PM
 * To change this template use File | Settings | File Templates.
 */
public class RenderSitemap extends RenderSitemapXml {
    private final static String[] pages = { "/", "/tin-tuc", "/contact_vn",
            "/contact_en", "/aboutUs_vn", "/aboutUs_en"};

    public RenderSitemap(List<App> allApp, List<AppGroup> allAppGroup, List<Topic> allTopic) {
        super(getDocument(allApp, allAppGroup, allTopic));
    }

    private static Document getDocument(List<App> allApp, List<AppGroup> allAppGroup, List<Topic> allTopic) {
        String currentLang = Lang.get();
        // The main language for my site is Spanish
        Lang.set("vn");

        Document doc = createSiteMapDocument();
        Element root = doc.getDocumentElement();

        /*String homeLoc = Router.getFullUrl("ServiceTypes.all");
        root.appendChild(createUrl(doc, homeLoc, "daily", 0.9));*/

        for (String page : pages) {
            String pageLoc = Http.Request.current().getBase() + page;
            root.appendChild(createUrl(doc, pageLoc, "daily", 0.1));
        }

        for(AppGroup appGroup : allAppGroup) {
            Map<String, Object> args = new HashMap<String, Object>();
            args.put("code", appGroup.code);
            args.put("rewriteUrl", appGroup.getShortTitle());
            String loc = Router.getFullUrl("User.listAppInGroup", args);
            root.appendChild(createUrl(doc, loc, "weekly", 0.7));
        }

        for(App app : allApp) {
            Map<String, Object> args = new HashMap<String, Object>();
            args.put("code", app.code);
            args.put("rewriteUrl", app.getShortTitle());
            String loc = Router.getFullUrl("User.listTopicInApp", args);
            root.appendChild(createUrl(doc, loc, "weekly", 0.7));
        }

        for(Topic topic : allTopic) {
            Map<String, Object> args = new HashMap<String, Object>();
            args.put("code", topic.code);
            args.put("rewriteUrl", topic.getShortTitle());
            String loc = Router.getFullUrl("User.topic", args);
            root.appendChild(createUrl(doc, loc, "weekly", 0.7));
        }

        Lang.set(currentLang);
        return doc;
    }
}
